package com.kancy.jemeter.functions;

import com.kancy.jemeter.util.IDCardUtil;
import com.kancy.jemeter.util.MessageUtil;
import com.kancy.jemeter.util.StringUtil;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.LinkedList;
import java.util.List;

/**
 * @author kancy
 * @date 2019/6/29 17:17
 */
public class IDCardFunction extends JemeterAbstractFunction {

	public IDCardFunction() {
		super(0,5);
	}

	@Override
	public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
		String result = getResult();
		setJMeterVariable(2, result);
		if (hasParameter(3)){
			setJMeterVariable(3, result.substring(0, 6));
		}
		if (hasParameter(4)){
			setJMeterVariable(4, IDCardUtil.parseAddress(result));
		}
		return result;
	}


	private static final List<String> argumentDesclist = new LinkedList();

	static {
		argumentDesclist.add(MessageUtil.getResString("__idcard.district.message"));
		argumentDesclist.add(MessageUtil.getResString("__idcard.birthday.message"));
		argumentDesclist.add(MessageUtil.getResString("varname.message"));
		argumentDesclist.add(MessageUtil.getResString("__idcard.district.varname.message"));
		argumentDesclist.add(MessageUtil.getResString("__idcard.address.varname.message"));
	}
	@Override
	public List<String> getArgumentDesc() {
		return argumentDesclist;
	}

	public String getResult() {
		String district = null;
		String birthday = null;
		if(hasParameter(0)){
			district = getParameter(0);
		}
		if(hasParameter(1)){
			birthday = getParameter(1);
		}
		if(StringUtil.isEmpty(district)){
			district = IDCardUtil.getdistrictcode();
		}
		if(StringUtil.isEmpty(birthday)){
			birthday = IDCardUtil.getBirthday();
		}
		return IDCardUtil.create(district, birthday);
	}

}
